package com.zde.moriserver.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zde.moriserver.entity.GiftEntity;
import com.zde.moriserver.entity.GiftEntityRepository;
import com.zde.moriserver.entity.UsersEntity;
import com.zde.moriserver.entity.UsersEntityRepository;
import com.zde.moriserver.result.Result;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/live")
@Tag(name = "直播接口接口")
public class LiveController {

    @Autowired
    private UsersEntityRepository usersEntityRepository;
    @Autowired
    private GiftEntityRepository giftEntityRepository;

    /**
     * 获取用户个人信息
     */
    @GetMapping("/getUserInfo")
    public Result getUserInfo(){
        int loginIdAsInt = StpUtil.getLoginIdAsInt();
        UsersEntity usersEntity = usersEntityRepository.getById(loginIdAsInt);
        return Result.success(usersEntity);
    }


    @PostMapping("/givingGifts/{giftId}")
    public Result givingGifts(@PathVariable String giftId){
        //通过token获取用户id
        int loginIdAsInt = StpUtil.getLoginIdAsInt();
        UsersEntity usersEntity = usersEntityRepository.getById(loginIdAsInt);
        //通过礼物id查询礼物信息
        GiftEntity giftEntity = giftEntityRepository.getById(giftId);
        //判断钱够不够
        if(usersEntity.getMoney() < giftEntity.getMoney()){
            return Result.fail("您的余额不足，请充值");
        }

        // 扣减余额
        long newMoney = usersEntity.getMoney() - giftEntity.getMoney().longValue();
        usersEntity.setMoney(newMoney);

        //贡献值 contribution
        usersEntity.setContribution(usersEntity.getContribution() + giftEntity.getMoney().longValue());

        /**
         * 当刷了一个很大的礼物 比如 9999
         * 可以连续升多级的情况，每一级的经验不清0
         */
        // 礼物经验
        Long experience = giftEntity.getMoney().longValue();
        // 1、直接把已有经验和礼物经验加起来
        usersEntity.setExperience(usersEntity.getExperience() + experience);
        do {
            /**
             * 基础信息
             */
            //等级
            Integer grade = usersEntity.getGrade();
            //等级 * 等级 * 100 + 等级 = 当前等级升级需要的经验
            Integer experienceUp = grade * grade * 100 + grade;
            //已有经验
            Long experience1 = usersEntity.getExperience();
            /**
             * 判断是否该升级
             */
            if(experienceUp < experience1){
                //等级 + 1
                usersEntity.setGrade(usersEntity.getGrade() + 1);
            }else{
                break;
            }
        }while (true);

        //更新
        usersEntityRepository.updateById(usersEntity);


        /**
         * 发送广播通知有人刷礼物
         * TODO 下学期学完微服务后填坑
         */
        return Result.success(giftEntity);

    }


    /**
     * 查询排行榜 前20个人
     * @return
     */
    @GetMapping("/giftRanking")
    public Result giftRanking(){
        //条件构造器
        QueryWrapper<UsersEntity> qw = new QueryWrapper<>();
        //构建条件
        qw.orderByDesc("contribution");
        //分页查询
        Page<UsersEntity> page = usersEntityRepository.page(new Page<>(1,20),qw);
        return Result.success(page);
    }


}
